﻿IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[SelectLogServicioBySearch]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
	DROP PROCEDURE [dbo].[SelectLogServicioBySearch]
GO

CREATE PROCEDURE [dbo].[SelectLogServicioBySearch]
(
	@Descripcion varchar(50) = null,
	@Fecha datetime,
	@IdTipo int = null,
	@IdEstado int = null
)
AS
BEGIN

	SELECT
	[IdLog],
	[IdTipo],
	[FechaAlerta],
	[IdLote],
	[IdInsumo],
	[Descripcion],
	[FechaCreacion],
	[IdEstado]
	FROM [LogServicio]
	WHERE FechaAlerta = @Fecha AND
	Case 
		when @Descripcion is null then 1
		when Descripcion like '%'+@Descripcion+'%' then 1
		else 0
	End = 1 and
	Case 
		when @IdTipo is null then 1
		when IdTipo = @IdTipo then 1
		else 0
	End = 1 and
	Case 
		when @IdEstado is null then 1
		when IdEstado = @IdEstado then 1
		else 0
	End = 1 

END

GO
